package de.baumann.browser.fragments;

import android.content.Intent;
import android.net.Uri;
import android.support.design.widget.BottomSheetDialog;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import java.util.List;

import de.baumann.browser.R;
import de.baumann.browser.adapter.Adapter_Record;
import de.baumann.browser.base.BaseOdinFragment;
import de.baumann.browser.db.Record;
import de.baumann.browser.db.RecordAction;
import de.baumann.browser.units.BrowserUnit;
import de.baumann.browser.views.widget.OdinToast;

public abstract class BaseBHFragment extends BaseOdinFragment {
    protected ListView mListView;
    protected RecordAction action;
    protected List<Record> list;

    @Override
    public int getLayout() {
        return R.layout.fragment_bh;
    }

    @Override
    public void initView(View view) {
        mListView = view.findViewById(R.id.bh_list);
        action = new RecordAction(getActivity());
        action.open(false);
        loadData();
        action.close();
        Adapter_Record adapter = new Adapter_Record(getActivity(), list);
        mListView.setAdapter(adapter);
        mListView.setOnItemClickListener((parent, view12, position, id) -> {
            Intent intent = new Intent("sc_odin");
            intent.setData(Uri.parse(list.get(position).getURL()));
            startActivity(intent);
        });
    }

    protected abstract void loadData();

    private BottomSheetDialog bottomSheetDialog;

    protected void showListMenu(final Adapter_Record adapterRecord, final List<Record> recordList, final int location, int flag) {

        final Record record = recordList.get(location);

        bottomSheetDialog = new BottomSheetDialog(getActivity());
        View dialogView = View.inflate(getActivity(), R.layout.dialog_menu_context, null);
        ViewGroup tv2MenuDelete = dialogView.findViewById(R.id.tv2_menu_delete);
        tv2MenuDelete.setOnClickListener(v -> {
            bottomSheetDialog.cancel();
            bottomSheetDialog = new BottomSheetDialog(getActivity());
            View dialogView1 = View.inflate(getActivity(), R.layout.dialog_action, null);
            TextView textView = dialogView1.findViewById(R.id.dialog_text);
            textView.setText(R.string.toast_titleConfirm_delete);
            Button actionOk = dialogView1.findViewById(R.id.action_ok);
            actionOk.setOnClickListener(view -> {
                RecordAction action = new RecordAction(getActivity());
                action.open(true);
                if (flag == BrowserUnit.FLAG_BOOKMARKS) {
                    action.deleteBookmark(record);
                } else {
                    action.deleteHistory(record);
                }
                action.close();
                recordList.remove(location);
                adapterRecord.notifyDataSetChanged();
                bottomSheetDialog.cancel();
                OdinToast.show(getActivity(), getString(R.string.toast_delete_successful));
            });
            Button actionCancel = dialogView1.findViewById(R.id.action_cancel);
            actionCancel.setOnClickListener(view -> bottomSheetDialog.cancel());
            bottomSheetDialog.setContentView(dialogView1);
            bottomSheetDialog.show();
        });

        ViewGroup tv2MenuEdit = dialogView.findViewById(R.id.tv2_menu_edit);

        if (flag == BrowserUnit.FLAG_BOOKMARKS) {
            tv2MenuEdit.setVisibility(View.VISIBLE);
        } else {
            tv2MenuEdit.setVisibility(View.GONE);
        }

        tv2MenuEdit.setOnClickListener(v -> {
            bottomSheetDialog.cancel();
            showEditDialog(adapterRecord, recordList, location);
        });
        bottomSheetDialog.setContentView(dialogView);
        bottomSheetDialog.show();
    }

    protected void showEditDialog(final Adapter_Record adapterRecord, List<Record> recordList, int location) {
    }

    @Override
    public boolean showBindEth() {
        return false;
    }
}
